/**
 * 
 */
package travibot.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

/**
 * Class representing one village. There can be more villages if the player has
 * them.
 * 
 * @author Petr Macek
 * 
 */
public class Village {

    private static Logger log = Logger.getLogger(Village.class);

    private final int id;

    private String name;

    private String detailLink;

    private Map<Integer, Position> positions;

    private Production production;

    private Stock stock;

    private Set<Structure> structures;

    private boolean initialized;

    public Village(int id) {

        super();
        log.info("Village " + id + " init started");
        this.id = id;
        this.positions = new HashMap<Integer, Position>();
        this.structures = new HashSet<Structure>();
        this.stock = new Stock();
        this.production = new Production();
        log.info("Village " + id + " init finished");
    }

    public void addPosition(Position position) {

        if(!positions.containsKey(position.getId())){
            positions.put(position.getId(), position);
            if (position.isDeveloped()) {
                Structure struct = ((DevelopedPosition)position).getBuiltStructure();
                if (struct != null && !structures.contains(struct)) {
                    structures.add(struct);
                }
            }
        }
    }

    public void addStructure(Structure structure) {

        structures.add(structure);
        if (structure.getPosition() != null && !positions.containsKey(structure.getPosition().getId())) {
            positions.put(structure.getPosition().getId(), structure.getPosition());
        }
    }

    public Stock getStock() {

        return stock;
    }

    public Production getHourlyProduction() {

        return production;
    }

    public int getId() {

        return id;
    }

    public String getName() {

        return name;
    }

    public Position getPosition(int id) {

        return positions.get(id);
    }

    public List<Position> getPositions() {

        List<Position> ret = null;
        Set<Integer> keys = positions.keySet();
        for (int key : keys) {
            if (ret == null) {
                ret = new ArrayList<Position>();
            }
            ret.add(positions.get(key));
        }
        return ret;
    }

    public List<Structure> getStructures() {

        List<Structure> ret = null;
        for (Structure key : structures) {
            if (ret == null) {
                ret = new ArrayList<Structure>();
            }
            ret.add(key);
        }
        return ret;
    }

    public void setName(String name) {

        this.name = name;
    }

    public String getDetailLink() {

        return detailLink;
    }

    public void setDetailLink(String detailLink) {

        this.detailLink = detailLink;
    }
    

    public boolean isInitialized() {

        return initialized;
    }

    public void setInitialized(boolean initialized) {

        this.initialized = initialized;
    }

}
